package top.fengyulou.xye.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.fengyulou.xye.dao.UserRepository;
import top.fengyulou.xye.entity.User;

import java.util.Date;

@Controller
@RequestMapping("/b/user")
public class DemoController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestParam String loginName, @RequestParam String password) {
        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setAddTime(new Date());
        userRepository.save(user);
        return "Saved";
    }

    @RequestMapping("/all")
    @ResponseBody
    public Iterable<User> all() {
        return userRepository.findAll();
    }

    @RequestMapping("/page")
    @ResponseBody
    public Page<User> page() {
        Page<User> page = userRepository.findAll(PageRequest.of(0, 20));
        System.out.println(page.hasContent());
        return page;
    }

}
